﻿using AdventureWorks.DataAccess.repository;
using NUnit.Framework;
using AdventureWorks.DataAccess.SearchCriterias;
using System.Linq;
using AdventureWorks.Domain;
using System;
using NHibernate;
using AdventureWorks.DataAccess.Utils;

namespace AdventureWorks.DataAccess.Test.repository
{

    [TestFixture]
    public class VendorRepositoryTest : RepositoryTestBase<VendorRepository>
    {

        [Test]
        public void When_Passing_NullParameters_Should_Return_All_Vendors()
        {
            var criteria = new VendorSearchCriteria();
            var vendors = repository.GetVendors(session, criteria);
            Assert.AreEqual(105, vendors.Count<Vendor>());
        }

        [Test]
        public void When_Passing_Non_Active_Should_Return_Non_ActiveOnly()
        {
            var criteria = new VendorSearchCriteria { Active = false };
            var vendors = repository.GetVendors(session, criteria);
            Assert.AreEqual(1, vendors.Count<Vendor>());
        }

        [Test]
        public void When_Passing_Non_Prefered_Should_Return_Non_Prefered_Only()
        {
            var criteria = new VendorSearchCriteria { PreferredVendorStatus = false };
            var vendors = repository.GetVendors(session, criteria);
            Assert.AreEqual(1, vendors.Count<Vendor>());
        }

        [Test]
        public void When_Passing_Name_Should_Return_Only_One()
        {
            var criteria = new VendorSearchCriteria { Name = "Recreation Place" };
            var vendors = repository.GetVendors(session, criteria);
            Assert.AreEqual(1, vendors.Count<Vendor>());
        }


        [Test]
        public void Search_By_AccountNUmber()
        {
            var criteria = new VendorSearchCriteria { AccountNumber = "GARDNER0001" };
            var vendors = repository.GetVendors(session, criteria);
            Assert.AreEqual(1, vendors.Count<Vendor>());
        }

        [Test]
        public void Search_By_CreditRating()
        {
            var criteria = new VendorSearchCriteria { CreditRating = 4 };
            var vendors = repository.GetVendors(session, criteria);
            Assert.AreEqual(2, vendors.Count<Vendor>());
        }

        // ******************************* Add vendor ******************************/
        [Test]
        public void Add_Vendor_Test()
        {
            var date = new DateTime(2002, 11, 22);
            SystemTime.Now = () => date;
            var vendor = new Vendor { AccountNumber = "acc number", Name = "test name", CreditRating = 2, Active = true, PreferredVendorStatus = true};
            repository.SaveVendor(session, vendor);
            var saveVendor = repository.GetVendors(session, new VendorSearchCriteria { AccountNumber = "acc number" });
            Assert.AreEqual(1, saveVendor.Count<Vendor>());
            var vendorList = saveVendor.ToList();
            Assert.AreEqual("acc number", vendorList[0].AccountNumber);
            Assert.AreEqual("test name", vendorList[0].Name);
            Assert.AreEqual(2, vendorList[0].CreditRating);
            Assert.IsTrue(vendorList[0].Active);
            Assert.IsTrue(vendorList[0].PreferredVendorStatus);
            Assert.AreEqual(date, vendorList[0].ModifiedDate);
        }

        [Test]
        public void Update_Vendor_Test()
        {
            var vendor = repository.GetVendors(session, new VendorSearchCriteria { AccountNumber = "TRIKES0001" });
            Assert.AreEqual(1, vendor.Count<Vendor>());
            var vendorEntity = vendor.ToList()[0];
            Assert.AreEqual("TRIKES0001", vendorEntity.AccountNumber);
            var id = vendorEntity.Id;
            var newVendor = new Vendor { Id = id, AccountNumber = "TRIKES0xxx", Name = "A Name", PreferredVendorStatus = false, Active = false, CreditRating = 1};
            repository.SaveVendor(session, newVendor);
            var vendors = repository.GetVendors(session, new VendorSearchCriteria { AccountNumber = "TRIKES0xxx" });
            Assert.AreEqual(1, vendors.Count<Vendor>());
        }
    }
}
